package com.zzxy.spring.aspect;

import com.zzxy.spring.service.IteacherService;
import com.zzxy.spring.service.impl.TeacherServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyBeanFactory {
    public static IteacherService createTeacherService(){
        IteacherService teacherService = new TeacherServiceImpl();
        MyAspect aspect = new MyAspect();
        IteacherService proxyTeacherService = (IteacherService) Proxy.newProxyInstance(MyBeanFactory.class.getClassLoader(), teacherService.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                aspect.before();
                Object obj = method.invoke(teacherService, args);
                aspect.after();
                return obj;
            }
        });
        return proxyTeacherService;

    }
}

